package ace.cmp.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;

/**
 * @author Caspar 提供 {@link java.util.stream.Stream} 常用扩展功能
 */
public final class AceStreamUtils {

  /**
   * 安全转 list 为 stream
   *
   * @param collections
   * @param <T>
   * @return
   */
  public static <T> Stream<T> ofNullable(Collection<T> collections) {
    return Stream.ofNullable(collections).flatMap(Collection::stream);
  }

  public static <T> List<T> forCount(int count, Function<Integer, T> function) {
    List<T> result = new ArrayList<>(count);
    for (int i = 0; i < count; i++) {
      result.add(function.apply(i));
    }
    return result;
  }
}
